/**
 * Copyright Choonho Son ,choonho.son(at)kt(dot).com
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.oracle.em12c.command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Hashtable;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;


import javax.net.ssl.SSLSocketFactory;

import org.apache.commons.codec.binary.Base64;

import com.oracle.em12c.api.*;


public class Command {
	
	String base_url = null;
	String uri = null;
	String auth = null;
	
    public Command() {
    }
    
    public Command(String base_url, String id, String pw) {
    	this.base_url = base_url;
    	this.setAuth(id, pw);
    }
    
    public void setBaseURL(String base_url) {
    	this.base_url = base_url;
    }
    
    public void setAuth(String id, String pw) {
    	byte[] clearText = ( id + ":" + pw).getBytes();
        this.auth = new String(Base64.encodeBase64(clearText));
        System.out.println("Encoded pw:" + this.auth);
    }

    private HttpsURLConnection createSession(String uri) throws IOException {
    	SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    	URL url = new URL(this.base_url + uri);
    	System.out.println("URI:" + url.toString());
    	
    	HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
    	conn.setHostnameVerifier(new HostnameVerifier()  
        {        
            public boolean verify(String hostname, SSLSession session)  
            {  
                return true;  
            }  
        });
    	conn.setSSLSocketFactory(sslsocketfactory);
    	conn.setRequestProperty( "Authorization", "Basic " + this.auth);
    	conn.setRequestProperty("X-Cloud-client-Specification-Version", "0.1");
        conn.setRequestProperty("Host","em.ucloud.com");
    	return conn;
    }
    

    
    public Element createCmd(String cmd, Hashtable params) throws IOException {
    	ApiCommand api;
    	
    	HttpsURLConnection conn = this.createSession((String)params.get("uri"));
    	if (cmd.equals("GetCloud")) {
    		api = new GetCloud(conn);
    		return (Cloud)api.execute(params);
    	}
    	else if (cmd.equals("GetZone")) {
    		api = new GetZone(conn);
    		return (Zone)api.execute(params);
    	}
    	else if (cmd.equals("GetVDC")) {
    		api = new GetVDC(conn);
    		return (VDC)api.execute(params);
    	}
    	else if (cmd.equals("GetVM")) {
    		api = new GetVM(conn);
    		return (VM)api.execute(params);
    	}
    	else if (cmd.equals("GetNetworkInterface")) {
    		api = new GetNetworkInterface(conn);
    		return (NetworkInterface)api.execute(params);
    	}
    	else if (cmd.equals("GetServiceTemplate")) {
    		api = new GetServiceTemplate(conn);
    		return (ServiceTemplate)api.execute(params);
    	}
    	
    	else if (cmd.equals("ControlVM")) {
    		api = new ControlVM(conn);
    		return (VMStatus)api.execute(params);
    	}
    	
    	else if (cmd.equals("DeployVM")) {
    		api = new DeployVM(conn);
    		return (VMStatus)api.execute(params);
    	}
    	
    	return null;
    	
    	
    }
}
